// 使用iOS风格页面跳转动画
import 'package:flutter/cupertino.dart';
// 使用普通风格、自定义风格的页面跳转动画
// import 'package:flutter/material.dart';
// 路由页面
import '../pages/home.dart';
import '../pages/goodsManagePage/index.dart';
import '../pages/goodsManagePage/detail.dart';
import '../pages/goodsManagePage/add_goods.dart';
import '../pages/goodsManagePage/statistics.dart';
import '../pages/scan.dart';

class RouterTable {
  // 白名单
  static List<String> whiteList = [loginPath, notFoundPath];
  static String splashPath = 'splash';
  static String loginPath = 'login';
  static String homePath = '/';
  static String notFoundPath = '404';

  static Map<String, WidgetBuilder> routeTables = {
    //404页面
    // notFoundPath: (_) => const NotFound(),
    // //启动页
    // splashPath: (_) => const SplashPage(),
    // //登录
    // loginPath: (_) => const LoginPage(),
    //首页
    homePath: (_) => const MyHomePage(),
    '/goodsManagePage': (_) => const GoodsManagePage(),
    '/goodsDetailPage': (_) => const GoodsDetailPage(),
    '/goodAddPage': (_) => const GoodsAddpage(),
    '/statisticPage': (_) => const StatisticPage(),
    '/scanPage': (_) => QRScannerPage(
          config: QRScannerPageConfig(),
        ),
    // '/homePage': (_) => const HomePage()
  };
  // 跳转前判断 依据路由名称判断
  static String? routeBeforeHook(RouteSettings settings) {
    String token = 'val';
    // 如果在白名单当中不校验
    if (whiteList.contains(settings.name)) {
      return settings.name;
    } else {
      if (token == '') {
        return loginPath;
      } else {
        return settings.name == loginPath ? loginPath : settings.name;
      }
    }
  }

  ///路由拦截
  static Route onGenerateRoute<T extends Object>(RouteSettings settings) {
    // 使用ios
    return CupertinoPageRoute<T>(
      settings: settings,
      builder: (context) {
        String name = routeBeforeHook(settings)!;
        Widget widget = routeTables[name] == null
            ? routeTables[notFoundPath]!(context)
            : routeTables[name]!(context);
        return widget;
      },
    );
  }
}
